public class num1143 {
    public int longestCommonSubsequence(String text1, String text2) {
        int len1 = text1.length();
        int len2 = text2.length();
        int[][] arr = new int[len2 + 1][len1 + 1];
        for (int i = 1; i <= len2; i++) {
            for (int j = 1; j <= len1; j++) {
                if(text2.charAt(i - 1) == text1.charAt(j - 1)) {
                    arr[i][j] = 1 + arr[i - 1][j - 1];
                }else {
                    arr[i][j] = Math.max(arr[i - 1][j],arr[i][j - 1]);
                }
            }
        }
        return arr[len2][len1];
    }
}
